package cn.iocoder.yudao.module.system.controller.admin.merchant;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentReqVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.ParticipationReqVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.AdminGetMerchantRespVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantReqVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*;
import cn.iocoder.yudao.module.system.convert.merchant.MerchantConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.system.service.merchant.MerchantService;
import cn.iocoder.yudao.module.system.service.merchant.MerchantUserListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author 梁富贵
 * @Date 2024/10/9
 **/
@Tag(name = "管理员后台 - 商家获取")
@RestController
@RequestMapping("/system/admin-merchant")
@Validated
public class MerchantGettingController {
    @Resource
    private MerchantUserListService merchantUserListService;

    @Resource
    private MerchantService merchantService;


    @PostMapping("/page")
    @Operation(summary = "获取商家列表")
    public CommonResult<PageResult<AdminGetMerchantRespVO>> getMerchantList(@RequestBody MerchantReqVO reqVO) {
        PageResult<MerchantDO> pageResult = merchantUserListService.getMerchantList(reqVO);
        MerchantConvert convert = new MerchantConvert();
        if(CollUtil.isEmpty(pageResult.getList())){
            return success(new PageResult<>(pageResult.getTotal()));
        }
        return success(new PageResult<>(convert.convertList(pageResult.getList()),pageResult.getTotal()));
    }

    @GetMapping("/get")
    @Operation(summary = "获得商家")
    public CommonResult<AdminGetMerchantRespVO> getMerchant(@RequestParam("id") Long id) {
        AdminGetMerchantRespVO adminGetMerchantRespVO = merchantUserListService.getMerchant(id);
        return success(adminGetMerchantRespVO);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改商家状态")
    public CommonResult<Boolean> updateMerchantStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
        merchantUserListService.updateMerchantStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "修改商家")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody MerchantSaveReqVO reqVO) {
        merchantUserListService.updateMerchant(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商家")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        merchantUserListService.deleteMerchant(id);
        return success(true);
    }

    @Schema(description = "招商商家获取参与招商的合作商")
    @GetMapping("/getParticipationMerchant")
    public CommonResult<PageResult<MerchantDO>> getParticipationMerchant(@Valid ParticipationReqVO reqVO) {
        return success(merchantService.InvestmentGettingMerchantPage(reqVO));
    }

    @Schema(description = "参与招商商家获取招商商家")
    @GetMapping("/getInvestmentMerchant")
    public CommonResult<PageResult<MerchantDO>> getInvestmentMerchant(@Valid InvestmentReqVO reqVO) {
        return success(merchantService.ParticipationGettingMerchantPage(reqVO));
    }
}
